--[[
Author: wangxiaoyan
Date: 2020-11-03 16:35:36
LastEditTime: 2020-12-02 20:48:05
FilePath: \Lua\View\UILevel\UILevelStrategy.lua
--]]

--- TODO: 补充完整
---@class UILevelStrategy : UIBase
UILevelStrategy = UIBase.New(PanelResNames.UILevelStrategy)
local _M = UILevelStrategy

_M.PAGE_PASSLEVEL = 0   -- 通关视频
_M.PAGE_SHOWUP = 1      -- 上阵率

function _M.Init(fightType,stageId)
    _M.fightType = fightType or EnumConst.FightTypeConst.DUNGEON
    _M.stageId = stageId
    UIBase.Init(_M,func)
end

-- 创建
function _M:OnCreate()
    self.pageController = self:GetController("page")

    self.passLevelDynamicList = self:SetDynamicList(self:GetChild("passLevelList"),self.RefresPassLevelDynamicItem)
    self.showUpDynamicList = self:SetDynamicList(self:GetChild("showUpList"),self.RefresShowUpListDynamicItem)
    self:RegisterOnClick(self:GetChild("passLevelBtn"),self.ShowPassLevel)
    self:RegisterOnClick(self:GetChild("showUpBtn"),self.ShowShowUp)
    self:RegisterOnClick(self:GetChild("back"),self.OnBack)
    self:RegisterEvent(Config.EventType.LevelBattleRecord,self.ReceiLevelBattleRecord)

    --- TODO 用新的方式获取关卡记录
end

-- 销毁
function _M:OnDestroy()
    self.passLevelDynamicListItems = nil
    self.showUpDynamicListItems = nil
end

-- 显示
function _M:OnEnable()
end

-- 隐藏
function _M:OnDisable()

end

function _M:ShowPassLevel()
    self.pageController.selectedIndex = self.PAGE_PASSLEVEL
    local count = 0
    if self.recordlist then 
       count = #self.recordlist 
    end
    self.passLevelDynamicList.numItems = count
end

function _M:ShowShowUp()
    self.pageController.selectedIndex = self.PAGE_SHOWUP
    local count = 0
    if self.showUpList then 
       count = #self.showUpList 
    end
    self.showUpDynamicList.numItems = count
end

function _M:CreatePassLevelDynamicItem(index,itemObj)
    if self.passLevelDynamicListItems == nil then 
        self.passLevelDynamicListItems = {}
    end
    if self.passLevelDynamicListItems[index] then 
       return 
    end
    local item = {}
    self.passLevelDynamicListItems[index] = item
    item.name = self:GetChild("name",itemObj)
    item.icon = self:GetChild("head/icon",itemObj)
    item.time = self:GetChild("time",itemObj)
    self:RegisterOnClick(self:GetChild("battleBtn",itemObj),self.ClickPassLevelDynamicItem,self,index)
end
function _M:ClickPassLevelDynamicItem(index)
    self.recordlist[index].result = true
    showUI(PanelNames.UILevelRecode,self.fightType, self.recordlist[index])
end
function _M:RefresPassLevelDynamicItem(index,itemObj)
    self:CreatePassLevelDynamicItem(index,itemObj)

    local item = self.passLevelDynamicListItems[index]
    local data = self.recordlist[index]
    self:SetText(item.name,data.name)
    self:SetText(item.time,timeToTimeFormat_MS(data.time))
    local iconPath = logicMgr.HeroManager.GetHeroConfigbyModelId(data.headIcon).f_HeroIcon;
    self:SetIcon(item.icon,iconPath,EnumConst.ItemShowType.ICON)
end

function _M:CreateShowUpListDynamicItem(index,itemObj)
    if self.showUpDynamicListItems == nil then 
        self.showUpDynamicListItems = {}
    end
    if self.showUpDynamicListItems[index] then 
       return 
    end
    local item = {}
    self.showUpDynamicListItems[index] = item
    item.name = self:GetChild("name",itemObj)
    item.icon = self:GetChild("icon/icon",itemObj)
    item.rate = self:GetChild("rate",itemObj)
    item.process = self:GetChild("process",itemObj)
end
function _M:RefresShowUpListDynamicItem(index,itemObj)
    self:CreateShowUpListDynamicItem(index,itemObj)

    local item = self.showUpDynamicListItems[index]
    local data = self.showUpList[index]
    local config = logicMgr.HeroManager.GetHeroConfigbyModelId(data.headIcon)
    self:SetText(item.name,getLanguage(config.f_HeroName))
    self:SetIcon(item.icon,config.f_HeroIcon,EnumConst.ItemShowType.ICON)
    local rate = string.format("%d",data.count/self.totalShowUpCount * 100)
    self:SetText(item.rate,rate)
    item.process.value = rate
end

-- 列表战报
function _M:ReceiLevelBattleRecord(recordList)
    self.recordlist = recordList or {}
    -- 计算出场数据
    self:CalShowUpInfo()
    self:ShowPassLevel()
end

function _M:CalShowUpInfo()
    local showUpList = {}
    for key, value in pairs(self.recordlist) do
        if value.count == nil then 
           value.count = 0
        end
        if showUpList[value.headIcon] == nil then 
            showUpList[value.headIcon] = value
            showUpList[value.headIcon].count = 1
        else
            showUpList[value.headIcon].count = showUpList[value.headIcon].count + 1
        end
    end
    self.totalShowUpCount = 0
    self.showUpList = {}
    for key, value in pairs(showUpList) do
        self.totalShowUpCount = self.totalShowUpCount + value.count
        table.insert(self.showUpList,value)
    end
    table.sort(self.showUpList,function(a,b)
        return a.count > b.count
    end)
end

return _M